import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:sinosheets/components/normal_product_item.dart';
import 'package:sinosheets/components/no_data_widget.dart';

import 'logic.dart';

class CategoryProductPage extends StatefulWidget {
  const CategoryProductPage({Key? key}) : super(key: key);

  @override
  State<CategoryProductPage> createState() => _CategoryProductPageState();
}

class _CategoryProductPageState extends State<CategoryProductPage> {
  final CategoryProductLogic logic = Get.put(CategoryProductLogic());

  @override
  Widget build(BuildContext context) {
    return GetBuilder<CategoryProductLogic>(builder: (get) {
      return Scaffold(
        appBar: AppBar(
          title: Text(get.categoryName, style: TextStyle(fontSize: 20.r, fontWeight: FontWeight.bold)),
          centerTitle: true,
        ),
        body: Padding(
          padding: EdgeInsets.symmetric(horizontal: 10.r, vertical: 10.r),
          child: EasyRefresh(
            controller: get.controller,
            onRefresh: () async {
              await get.onRefresh();
            },
            onLoad: () async {
              await get.onLoad();
            },
            child: get.categoryProductList.isEmpty
                ? NoDataWidget.emptyList(
                    title: 'No Products Found',
                    description: 'No products found in this category. Please try another category or check back later.',
                    showButton: true,
                    buttonText: 'Refresh',
                    onButtonTap: () async {
                      await get.onRefresh();
                    },
                  )
                : GridView.builder(
                    shrinkWrap: true,
                    gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                      crossAxisCount: 2,
                      mainAxisSpacing: 16.r,
                      crossAxisSpacing: 16.r,
                      childAspectRatio: 0.63,
                    ),
                    itemCount: get.categoryProductList.length,
                    itemBuilder: (context, index) {
                      return NormalProductItem(item: get.categoryProductList[index]);
                    },
                  ),
          ),
        ),
      );
    });
  }

  @override
  void dispose() {
    Get.delete<CategoryProductLogic>();
    super.dispose();
  }
}
